local extension = Package:new("konpack__misc")
extension.extensionName = "konpack"

local U = require "packages/utility/utility"

Fk:loadTranslationTable{
  ["konpack__misc"] = "形形色色的人",
}

local konpack_kogasa = General:new(extension, "konpack_kogasa", "th_k__yokai", 3, 3, General.Female)
konpack_kogasa.trueName = "th_kogasa"
local konpack_jinxia = fk.CreateTriggerSkill{
  name = "konpack_jinxia",
  anim_type = "drawcard",
  events = {fk.EventPhaseProceeding},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(3)
    room:moveCards({
      ids = cards,
      toArea = Card.Processing,
      moveReason = fk.ReasonJustMove,
      skillName = self.name,
      proposer = player.id,
    })
    local cardtable = room:askForYiji(player, cards, room:getAlivePlayers(false), self.name, 3, 3, "#konpack_jinxia-yiji", cards)
    for pid, cids in pairs(cardtable) do
      if #cids ~= 0 then
        local numbers = table.filter(table.map(cids, function(cid)
          local c = Fk:getCardById(cid)
          return c and c.number or 0
        end), function(num) return num >= 1 end)
        room:setPlayerMark(room:getPlayerById(pid), "@konpack_jinxia-turn", math.max(table.unpack(numbers)))
      end
    end
  end,

  refresh_events = {fk.CardUsing},
  can_refresh = function(self, event, target, player, data)
    if not (target and target == player and data.card and data.card.number >= 1) then return end
    local mark = player:getMark("@konpack_jinxia-turn")
    return mark >= 1 and data.card.number <= mark
  end,
  on_refresh = function(self, event, target, player, data)
    data.toCard = nil
    data.tos = {}
  end
}
local konpack_tiaoxi = fk.CreateActiveSkill{
  name = "konpack_tiaoxi",
  anim_type = "offensive",
  can_use = function(self, player)
    if player:usedSkillTimes(self.name) ~= 0 then return end
    local slash = Fk:cloneCard("slash")
    slash.skillName = self.name
    return player:canUse(slash, {bypass_distances = true, bypass_times = true})
  end,
  card_num = 0,
  card_filter = Util.FalseFunc,
  min_target_num = 1,
  target_filter = function(self, to_select, selected)
    local slash = Fk:cloneCard("slash")
    slash.skillName = self.name
    return Self:canUseTo(slash, Fk:currentRoom():getPlayerById(to_select), {bypass_distances = true, bypass_times = true})
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local dat = room:useVirtualCard("slash", nil, player, table.map(effect.tos, Util.Id2PlayerMapper), self.name)
    if not dat.cardsResponded then return end
    local cards = table.simpleClone(dat.cardsResponded)
    local targets = {}
    room.logic:getEventsOfScope(GameEvent.UseCard, 1, function(e)
      local use = e.data[1]
      local user = room:getPlayerById(use.from)
      if #cards == 0 then return true end
      if table.contains(cards, use.card) and use.responseToEvent and use.toCard == dat.card and user and user:isAlive() and user ~= player then
        table.removeOne(cards, use.card)
        table.insert(targets, user)
      end
      return false
    end, Player.HistoryTurn)
    if #targets ~= 0 then
      for _, p in ipairs(targets) do
        if room:askForSkillInvoke(p, self.name, nil, "#konpack_tiaoxi-vs:" .. player.id) then
          room:useVirtualCard("slash", nil, p, player, "konpack_tiaoxi")
        end
      end
    end
  end
}
local konpack_sanyin = fk.CreateTriggerSkill{
  name = "konpack_sanyin",
  anim_type = "defensive",
  frequency = Skill.Compulsory,
  events = {fk.TargetConfirming},
  can_trigger = function(self, event, target, player, data)
    if target == player and data.card.trueName == "slash" and player:hasSkill(self) then
      local room = player.room
      local from = room:getPlayerById(data.from)
      if not from or from.dead or from.hp <= player.hp then return false end
      local mark = player:getMark("konpack_sanyin_record-turn")
      local use_event = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
      if use_event == nil then return false end
      if mark == 0 then
        room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
          local use = e.data[1]
          if use.card.trueName == "slash" and
          table.contains(TargetGroup:getRealTargets(use.tos), player.id) then
            mark = e.id
            room:setPlayerMark(player, "konpack_sanyin_record-turn", mark)
            return true
          end
        end, Player.HistoryTurn)
      end
      return mark == use_event.id
    end
  end,
  on_use = function(self, event, target, player, data)
    local tos = AimGroup:getAllTargets(data.tos)
    for _, pid in ipairs(tos) do
      table.insertIfNeed(data.nullifiedTargets, pid)
    end
  end,
}
konpack_kogasa:addSkill(konpack_jinxia)
konpack_kogasa:addSkill(konpack_tiaoxi)
konpack_kogasa:addSkill(konpack_sanyin)
Fk:loadTranslationTable{
  ["konpack_kogasa"] = "多多良小伞",
  ["#konpack_kogasa"] = "被遗忘之伞",
  ["designer:konpack_kogasa"] = "弃置伞",
  ["illustrator:konpack_kogasa"] = "山鳥おふう",
  ["konpack_jinxia"] = "惊吓",
  [":konpack_jinxia"] = "准备阶段，你可以亮出牌堆顶三张牌，分配之并记录其获得的最大点数直到本回合结束；一名角色使用点数不大于记录的牌时，此牌无效。",
  ["konpack_tiaoxi"] = "跳袭",
  [":konpack_tiaoxi"] = "出牌阶段限一次，你可视为对任意名角色使用一张无次数限制的【杀】，响应此【杀】的角色可以视为对你使用一张【杀】。",
  ["konpack_sanyin"] = "伞荫",
  [":konpack_sanyin"] = "锁定技，你每回合首次成为【杀】的目标后，若你的体力值小于其，此【杀】无效。",
  ["#konpack_jinxia-yiji"] = "惊吓：你须分配这些牌",
  ["#konpack_tiaoxi-vs"] = "跳袭：你可以视为对 %src 使用一张【杀】",
  ["@konpack_jinxia-turn"] = "惊吓",
}


return extension